import store from "@/store";
import {
	getStorage
} from "./auth";

export function getWxLoginCode() {
	return new Promise((resolve, reject) => {
		wx.login({
			timeout: 10 * 1000,
			success: (res) => {
				if (res.code) {
					resolve(res.code);
				} else {
					uni.showToast({
						title: '获取登录信息失败',
						mask: true
					})
					reject()
				}
			},
			fail: () => {
				uni.showToast({
					title: '获取登录信息失败',
					mask: true
				})
				reject()
			}
		})
	})
}
export function checkLogin(back = true) {
	const hasToken = getStorage('token');
	const user = getStorage('user');
	if (!hasToken || !user) {
		uni.showModal({
			title: '提示',
			content: '需要先登录！',
			cancelText: '先逛逛',
			confirmText: '去登录',
			success(res) {
				if (res.confirm) {
					uni.switchTab({
						url: '/pages/mine/index'
					})
				} else {
					if (back) {
						uni.switchTab({
							url: '/pages/index/index'
						})
					}

				}
			}
		})
		return false;
	}
	return true;
}
/**
 * @description 判断是否登录
 * @returns {boolean}
 */
export function isLogin() {
	return !!store.getters.user;
}